package com.jsmtr.rms.dao.impl;

import java.util.List;
import java.util.Map;

import com.jsmtr.rms.dao.DaoServiceSupper;
import com.jsmtr.rms.utils.DBHelperUtil;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@ComponentScan
public class DaoServiceImplSupper extends DaoServiceImpl implements DaoServiceSupper {


    @SuppressWarnings("rawtypes")
    @Override
    public boolean deleteByLike(Map<String, String> param, Class clss)
            throws Exception {
        return false;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public List listByLike(Map<String, String> param, List<String> orders, Class clss)
            throws Exception {
        try {
            String sql = "SELECT * FROM " + DBHelperUtil.getTableNameByClass(clss) + " where 1=1 and ";
            sql += getLikeParamStr(param).replaceAll("[,]", " and ");
            if (orders != null) {
                sql = sql + " order by " + getOrders(orders, clss);
            }
            System.out.println(sql);
            //List list=DBHelperUtil.fillDataInfo(dt,this.getDeclaringClass());
            //return list;
            return null;
        } catch (Exception e) {
            throw e;
        }
    }

    protected String getLikeParamStr(Map<String, String> param) {
        String result = "";
        for (String fieldName : param.keySet()) {
            String value = param.get(fieldName);
            if (null != value && !value.toString().equalsIgnoreCase("")) {
                if (!fieldName.equalsIgnoreCase("pkId") && !fieldName.equalsIgnoreCase("ISDELETE")) {
                    result += fieldName + " like '%" + value + "%',";
                }
            }
        }
        result = result.substring(0, result.length() - 1);
        return result;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    protected String getOrders(List orders, Class clss) {
        Map<String, String> m = DBHelperUtil.getTableClumns(clss);
        String clumns = "";
        for (String fieldName : (List<String>) orders) {
            clumns += m.get(fieldName) + ",";
        }

        return clumns.substring(0, clumns.length() - 1);
    }

}